package com.hp.gxw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hp.gxw.cache.CommonCache;
import com.hp.gxw.constant.BaseEntry;
import com.hp.gxw.constant.LayTableR;
import com.hp.gxw.constant.R;
import com.hp.gxw.entity.*;
import com.hp.gxw.service.ICostElementsPriceService;
import com.hp.gxw.service.ICostElementsService;
import com.hp.gxw.utils.ConditionUtil;
import com.hp.gxw.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangping
 * @since 2024-01-04
 */
@RestController
@RequestMapping("/manage/elements_price")
public class CostElementsPriceController extends CommonController{

    @Autowired
    private ICostElementsService iCostElementsService;

    @Autowired
    private ICostElementsPriceService iCostElementsPriceService;

    @Autowired
    public CommonCache commonCache;

    @PostMapping("/list/{happenMonth}")
    public LayTableR list(@PathVariable("happenMonth") String happenMonth, Integer page, Integer limit, String master) throws Exception {
        /**初始化发生月数据*/
        List<CostElements> elementsList = iCostElementsService.elementPriceInfo(happenMonth);
        List<CostElementsPrice> list = new ArrayList<>();
        for (CostElements costElements : elementsList) {
            if(costElements.getAmount() == null){
                CostElementsPrice costElementsPrice = new CostElementsPrice();
                costElementsPrice.setElementsId(costElements.getId());
                costElementsPrice.setAmount(BigDecimal.ZERO);
                costElementsPrice.setDisable(false);
                costElementsPrice.setHappenMonth(happenMonth);
                costElementsPrice.setCreateUser(ShiroUtil.getUserId());
                costElementsPrice.setCreateTime(new Date());
                list.add(costElementsPrice);
            }
        }
        if(list.size() > 0){
            iCostElementsPriceService.saveBatch(list);
        }

        QueryWrapper<Object> ew = ConditionUtil.convert(master, new QueryWrapper<>());
        ew.eq("happen_month", happenMonth);
        BaseEntry baseEntry = commonCache.init("cost_elements_price");
        IService<Object> iService = baseEntry.getIService();
        Page<Object> pageList = iService.page(new Page<>(page, limit), ew);
        List<BaseCommonDataDictionaryAndSet> collect = baseEntry.getSetList().stream().filter(e -> e.getQueryConstantId() != null).collect(Collectors.toList());
        List<Map> resultList = dataTransfer(pageList.getRecords(), collect);
        return LayTableR.result(0, pageList.getTotal(), resultList);
    }

    @PostMapping("/save")
    @ResponseBody
    public R save(@RequestBody List<CostElementsPrice> list) {
        try {
            for (CostElementsPrice costElementsPrice : list) {
                costElementsPrice.setCreateUser(ShiroUtil.getUserId());
                costElementsPrice.setCreateTime(new Date());
            }
            iCostElementsPriceService.updateBatchById(list);
        } catch (Exception e) {
            return R.error("系统异常");
        }
        return R.ok();
    }
}